package com.quanshu.exchange.ui.gui.message.chat.redpackage;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jungly.gridpasswordview.GridPasswordView;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseDialog;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.subjects.BehaviorSubject;

/**
 * Created by jye on 2018/1/17.
 */

public class DialogCheckPayPwd extends BaseDialog {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.points_num_text)
    TextView pointsNumText;
    @BindView(R.id.pay_pwd_edit)
    GridPasswordView payPwdEdit;

    public BehaviorSubject<String> getOkSubject() {
        return okSubject;
    }

    private BehaviorSubject<String> okSubject = BehaviorSubject.create();

    public DialogCheckPayPwd(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_check_pay_pwd);
        ButterKnife.bind(this);
        initListener();
        initView();
    }

    @Override
    public void init(@NonNull Context context) {
        Window window = getWindow();
        window.setWindowAnimations(R.style.BottomAnim);
        window.setGravity(Gravity.CENTER);
        //默认的Dialog只有5/6左右的宽度，改为全屏宽度,由dialog的布局自己来决定实际显示宽度
        window.getDecorView().setPadding(50, 0, 50, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    private void initView() {
        reset();
        setCancelable(false);
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(back, aVoid -> dismiss());

        payPwdEdit.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {
            }

            @Override
            public void onInputFinish(String psw) {
                okSubject.onNext(psw);
            }
        });
    }

    public void setPointsNumText(String text) {
        double d = Double.valueOf(text);
        DecimalFormat decimal_format = new DecimalFormat("0.00");
        pointsNumText.setText(decimal_format.format(d));
    }

    public void reset() {
        if (pointsNumText != null) {
            pointsNumText.setText("");
        }
        if (payPwdEdit != null) {
            payPwdEdit.clearPassword();
        }
    }

}
